home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / blankery / blanker / source / prefs.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  10KB  |  346 lines

  1. /*
  2.  *    Copyright (c) 1993 Michael D. Bayne.
  3.  *    All rights reserved.
  4.  *
  5.  *    Please see the documentation accompanying the distribution for distribution and disclaimer information.
  6.  */
  7.  
  8. #include <exec/types.h>
  9. #include <exec/memory.h>
  10.  
  11. #include <dos/dos.h>
  12. #include <workbench/startup.h>
  13. #include <workbench/workbench.h>
  14.  
  15. #include <libraries/iffparse.h>
  16. #include <libraries/reqtools.h>
  17.  
  18. #include <clib/exec_protos.h>
  19. #include <clib/dos_protos.h>
  20. #include <clib/reqtools_protos.h>
  21. #include <clib/graphics_protos.h>
  22. #include <clib/iffparse_protos.h>
  23. #include <clib/icon_protos.h>
  24. #include <clib/wb_protos.h>
  25. #include <clib/utility_protos.h>
  26.  
  27. #include <string.h>
  28. #include <stdlib.h>
  29.  
  30. #include "defs.h"
  31. #include "Blanker.h"
  32.  
  33. #define ID_PREF MAKE_ID('P','R','E','F')
  34. #define ID_PRHD MAKE_ID('P','R','H','D')
  35. #define ID_BLNK    MAKE_ID('B','L','N','K')
  36.  
  37. #define BPREFSIZE    524
  38.  
  39. UWORD RangeRand( UWORD );
  40.  
  41. #include "protos/main.h"
  42. #include "protos/winhand.h"
  43. #include "protos/messaging.h"
  44.  
  45. UBYTE *doPath( UBYTE *file, UBYTE *dir )
  46. {
  47.     static UBYTE path[256];
  48.  
  49.     CopyMem( dir, path, 108 );
  50.     if( !AddPart( path, file, 256 )) return( 0L );
  51.     return( path );
  52. }
  53.  
  54. UBYTE *longToStr( LONG num )
  55. {
  56.     static    UBYTE    out[32];
  57.         LONG    i = 32, j;
  58.  
  59.     while( num ) { out[--i] = num % 10; num /= 10; }
  60.     for( j = 0; j < 32 - i; j++ ) out[j] = '0' + out[i+j];
  61.     out[j] = 0;
  62.  
  63.     return( out );
  64. }
  65.  
  66. VOID prefError( LONG rc )
  67. {
  68.     const char *errMsgs[] = {    "IFF handle allocation failed. (1)",
  69.                     "Open() (for read) failed on pref file. (2)",
  70.                     "OpenIFF() (for read) failed on pref file. (3)",
  71.                     "Internal error (Context Node retrieval failed). (4)",
  72.                     "No BLNK chunk found in pref file. (5)",
  73.                     "Open() (for write) failed on pref file. (6)",
  74.                     "OpenIFF() (for write) failed on pref file. (7)"
  75.                 };
  76.  
  77.     rtEZRequest( "Error processing preferences file:\n\n%s", "Ok", 0L, BTAGS, errMsgs[rc-1] );
  78. }
  79.  
  80. LONG loadModulePrefs( UBYTE *path, struct BlankerPrefs *bPO )
  81. {
  82.     struct IFFHandle *bHandle;
  83.     struct ContextNode *top;
  84.     LONG rc = -1, err;
  85.  
  86.     if(!( bHandle = AllocIFF())) return( 1 );
  87.  
  88.     if( bHandle->iff_Stream = (ULONG)Open( path, MODE_OLDFILE )) {
  89.         InitIFFasDOS( bHandle );
  90.  
  91.         if( !OpenIFF( bHandle , IFFF_READ )) {
  92.             while( rc == -1 ) {
  93.                 err = ParseIFF( bHandle, IFFPARSE_RAWSTEP );
  94.                 switch( err ) {
  95.                 case IFFERR_EOC:
  96.                     if( top = CurrentChunk( bHandle )) {
  97.                         if(( top->cn_Type == ID_PREF )&&( top->cn_ID == ID_BLNK )) {
  98.                             ReadChunkBytes( bHandle, &(bPO->bp_Mode), BPREFSIZE );
  99.                             rc = 0;
  100.                         }
  101.                     } else rc = 4;
  102.                     break;
  103.                 case IFFERR_EOF:
  104.                     rc = 5;
  105.                 default:
  106.                     break;
  107.                 }
  108.             }
  109.             CloseIFF( bHandle );
  110.         } else rc = 3;
  111.         Close( bHandle->iff_Stream );
  112.     } else rc = 2;
  113.     FreeIFF( bHandle );
  114.  
  115.     return( rc );
  116. }
  117.  
  118. LONG saveModulePrefs( UBYTE *path, struct BlankerPrefs *bPO )
  119. {
  120.     struct IFFHandle *bHandle;
  121.     UBYTE prefHead[] = {0,0,0,0,0,0};
  122.     LONG rc = 0;
  123.  
  124.     if(!( bHandle = AllocIFF())) return( 1 );
  125.  
  126.     if( bHandle->iff_Stream = (ULONG)Open( path, MODE_NEWFILE )) {
  127.         InitIFFasDOS( bHandle );
  128.  
  129.         if( !OpenIFF( bHandle , IFFF_WRITE )) {
  130.             PushChunk( bHandle, ID_PREF, ID_FORM, IFFSIZE_UNKNOWN );
  131.  
  132.             PushChunk( bHandle, ID_PREF, ID_PRHD, 6 );
  133.             WriteChunkBytes( bHandle, prefHead, 6 );
  134.             PopChunk( bHandle );
  135.  
  136.             PushChunk( bHandle, ID_PREF, ID_BLNK, BPREFSIZE );
  137.             WriteChunkBytes( bHandle, &(bPO->bp_Mode), BPREFSIZE );
  138.             PopChunk( bHandle );
  139.  
  140.             PopChunk( bHandle );
  141.             CloseIFF( bHandle );
  142.         } else rc = 7;
  143.         Close( bHandle->iff_Stream );
  144.     } else rc = 6;
  145.     FreeIFF( bHandle );
  146.  
  147.     return( rc );
  148. }
  149.  
  150. VOID readBlankerToolTypes( UBYTE *path, struct BlankerPrefs *bPO )
  151. {
  152.     struct DiskObject    *bDO;
  153.     UBYTE            *tooltype;
  154.  
  155.     if( bDO = GetDiskObject( path )) {
  156.         if( tooltype = FindToolType( bDO->do_ToolTypes, "CX_PRIORITY" ))
  157.             bPO->bp_Priority = atoi( tooltype );
  158.         if( tooltype = FindToolType( bDO->do_ToolTypes, "CX_POPUP" ))
  159.             bPO->bp_PopUp = (ULONG)MatchToolValue( tooltype, "YES" );
  160.         if( tooltype = FindToolType( bDO->do_ToolTypes, "CX_POPKEY" ))
  161.             CopyMem( tooltype, bPO->bp_PopKey, 128 );
  162.         if( tooltype = FindToolType( bDO->do_ToolTypes, "BLANKKEY" ))
  163.             CopyMem( tooltype, bPO->bp_BlankKey, 128 );
  164.         if( tooltype = FindToolType( bDO->do_ToolTypes, "TIMEOUT" ))
  165.             bPO->bp_Timeout = 10*atoi( tooltype );
  166.         if( tooltype = FindToolType( bDO->do_ToolTypes, "RANDTIMEOUT" ))
  167.             bPO->bp_RandTimeout = atoi( tooltype );
  168.         if( tooltype = FindToolType( bDO->do_ToolTypes, "BLANKERDIR" ))
  169.             CopyMem( tooltype, bPO->bp_Dir, 128 );
  170.         if( tooltype = FindToolType( bDO->do_ToolTypes, "BLANKER" ))
  171.             CopyMem( tooltype, bPO->bp_Name, 128 );
  172.         FreeDiskObject( bDO );
  173.     }
  174.  
  175.     if( !bPO->bp_Timeout ) bPO->bp_Timeout = 1800;
  176. }
  177.  
  178. VOID writeBlankerToolTypes( UBYTE *path, struct BlankerPrefs *bPO, ULONG main )
  179. {
  180. #define    NUMTOOLTYPES        9
  181.     struct DiskObject    *bDO;
  182.     UBYTE            **oldToolTypes, *oldDefTool, *toolTypes[NUMTOOLTYPES+1], *defToolTypes[] = 
  183.                 { "DONOTWAIT", "CX_PRIORITY=", "CX_POPUP=", "CX_POPKEY=", "BLANKKEY=","TIMEOUT=",
  184.                 "RANDTIMEOUT=",    "BLANKERDIR=", "BLANKER=" };
  185.     LONG            i;
  186.  
  187.     for( i = 0; i < NUMTOOLTYPES; i++ )
  188.         if( toolTypes[i] = AllocVec( 160, MEMF_CLEAR )) strcpy( toolTypes[i], defToolTypes[i] );
  189.         else break;
  190.     if( i < NUMTOOLTYPES ) while( --i > -1 ) FreeVec( toolTypes[i] );
  191.     else {
  192.         strcat( toolTypes[1], longToStr( bPO->bp_Priority ));
  193.         strcat( toolTypes[2], bPO->bp_PopUp ? "YES" : "NO" );
  194.         strcat( toolTypes[3], bPO->bp_PopKey );
  195.         strcat( toolTypes[4], bPO->bp_BlankKey );
  196.         strcat( toolTypes[5], longToStr( bPO->bp_Timeout/10 ));
  197.         strcat( toolTypes[6], longToStr( bPO->bp_RandTimeout ));
  198.         strcat( toolTypes[7], bPO->bp_Dir );
  199.         strcat( toolTypes[8], bPO->bp_Name );
  200.         toolTypes[NUMTOOLTYPES] = NULL;
  201.  
  202.         if(!( bDO = GetDiskObject( path ))) bDO = GetDiskObjectNew( "ENVARC:sys/def_pref" );
  203.  
  204.         if( bDO ) {
  205.             oldToolTypes = bDO->do_ToolTypes;
  206.             if( main ) bDO->do_ToolTypes = toolTypes;
  207.             else bDO->do_ToolTypes = &(toolTypes[6]);
  208.  
  209.             oldDefTool = bDO->do_DefaultTool;
  210.             bDO->do_DefaultTool = "Blanker";
  211.  
  212.             PutDiskObject( path, bDO );
  213.  
  214.             bDO->do_ToolTypes = oldToolTypes;
  215.             bDO->do_DefaultTool = oldDefTool;
  216.  
  217.             FreeDiskObject( bDO );
  218.         }
  219.  
  220.         for( i = 0; i < NUMTOOLTYPES; i++ ) FreeVec( toolTypes[i] );
  221.     }
  222. }
  223.  
  224. LONG loadBlankerPrefs( UBYTE *file, UBYTE *dir, struct BlankerPrefs *bP )
  225. {
  226.     LONG rc;
  227.  
  228.     readBlankerToolTypes( doPath( file, dir ), bP );
  229.     rc = loadModulePrefs( doPath( file, dir ), bP );
  230.     loadModule( 0 );
  231.     if( BlankerWnd ) setPrefs( bP, Blanker_CNT );
  232.  
  233.     return( rc );
  234. }
  235.  
  236. VOID saveBlankerPrefs( UBYTE *file, UBYTE *dir, struct BlankerPrefs *bPO )
  237. {
  238.     writeBlankerToolTypes( doPath( file, dir ), bPO, 0L );
  239.     saveModulePrefs( doPath( file, dir ), bPO );
  240. }
  241.  
  242. VOID saveDefaultPrefs( struct BlankerPrefs *bPO )
  243. {
  244.     writeBlankerToolTypes( "PROGDIR:Blanker", bPO, 1L );
  245.     saveModulePrefs( "ENV:Blanker.prefs", bPO );
  246.     saveModulePrefs( "ENVARC:Blanker.prefs", bPO );
  247. }
  248.  
  249. LONG loadPrefsFromWBArgList( struct WBArg *WBArgs, ULONG numArgs, struct BlankerPrefs *bPO )
  250. {
  251.     LONG    i, rc = 0L;
  252. extern    UBYTE    prefDir[];
  253.  
  254.     for( i = 0; i < numArgs; i++, WBArgs++ )
  255.         if( WBArgs->wa_Name && NameFromLock( WBArgs->wa_Lock, prefDir, 108 ))
  256.             if(!( rc = loadBlankerPrefs( WBArgs->wa_Name, prefDir, bPO ))) break;
  257.     return( rc );
  258. }
  259.  
  260. LONG loadPrefsFromENVARC( struct BlankerPrefs *bPO )
  261. {
  262.     LONG rc;
  263.  
  264.     readBlankerToolTypes( "PROGDIR:Blanker", bPO );
  265.     rc = loadModulePrefs( "ENV:Blanker.prefs", bPO );
  266.     loadModule( 0 );
  267.     if( BlankerWnd ) setPrefs( bPO, Blanker_CNT );
  268.  
  269.     return( rc );
  270. }
  271.  
  272. VOID FreeRPList( struct RandomPrefsList *rPL )
  273. {
  274.     struct RandomPrefs *Temp;
  275.  
  276.     rPL->rpl_Date.ds_Days = 0L;
  277.     rPL->rpl_Date.ds_Minute = 0L;
  278.     rPL->rpl_Date.ds_Tick = 0L;
  279.     rPL->rpl_Number = 0;
  280.     while( rPL->rpl_Head ) {
  281.         Temp = rPL->rpl_Head;
  282.         rPL->rpl_Head = Temp->rp_Next;
  283.         FreeMem( Temp, sizeof( struct RandomPrefs ));
  284.     }
  285. }
  286.  
  287. VOID buildRandList( struct RandomPrefsList *rPrefs, UBYTE *dir )
  288. {
  289.     struct    FileInfoBlock *Blk;
  290.     struct    RandomPrefs *Current;
  291.     BPTR    lock;
  292.     LONG    len;
  293.  
  294.     if(( Blk = AllocDosObject( DOS_FIB, 0L ))&&( lock = Lock( dir, ACCESS_READ ))&&( Examine( lock, Blk ))) {
  295.         if( CompareDates( &rPrefs->rpl_Date, &Blk->fib_Date ) > 0 ) {
  296. #ifdef STDIO
  297.             printf( "Rand Pref list is out of date. Rebuilding.\n" );
  298. #endif
  299.             FreeRPList( rPrefs );
  300.             CopyMem( &Blk->fib_Date, &rPrefs->rpl_Date, sizeof( struct DateStamp ));
  301.             while( ExNext( lock, Blk )) {
  302.                 if( Blk->fib_DirEntryType < 0 ) {
  303.                     len = strlen( Blk->fib_FileName );
  304.                     if( Stricmp( &(Blk->fib_FileName[len-5]), ".info" )&&
  305.                     ( Current = AllocMem( sizeof( struct RandomPrefs ), MEMF_CLEAR ))) {
  306. #ifdef STDIO
  307.                         printf( "Adding %s to list.\n", Blk->fib_FileName );
  308. #endif
  309.                         CopyMem( Blk->fib_FileName, Current->rp_Name, 108 );
  310.                         rPrefs->rpl_Number += 1;
  311.                         Current->rp_Next = rPrefs->rpl_Head;
  312.                         rPrefs->rpl_Head = Current;
  313.                     }
  314. #ifdef STDIO
  315.                     else printf( "Skipping %s.\n", Blk->fib_FileName );
  316. #endif
  317.                 }
  318.             }
  319.             if( IoErr() != ERROR_NO_MORE_ENTRIES ) rPrefs->rpl_Number = 0;
  320.         }
  321. #ifdef STDIO
  322.         else printf( "Rand Pref list is up to date.\n" );
  323. #endif
  324.     }
  325.     if( Blk ) FreeDosObject( DOS_FIB, Blk );
  326.     if( !rPrefs->rpl_Number ) FreeRPList( rPrefs );
  327. }
  328.  
  329. VOID loadRandPrefs( struct RandomPrefsList *rPL, struct BlankerPrefs *bPO )
  330. {
  331.     UWORD            i, num;
  332.     struct RandomPrefs    *Current;
  333.     UBYTE            *dir = "SYS:Prefs/Presets/Blanker";
  334.  
  335.     buildRandList( rPL, dir );
  336.  
  337.     if( rPL->rpl_Number ) {
  338.         num = RangeRand(( UWORD )rPL->rpl_Number );
  339.         for( i = 0, Current = rPL->rpl_Head; i < num; ++i, Current = Current->rp_Next );
  340. #ifdef STDIO
  341.         printf( "Chose %d. Loading %s from %s.\n", num, Current->rp_Name, dir );
  342. #endif
  343.         loadBlankerPrefs( Current->rp_Name, dir, bPO );
  344.     }
  345. }
  346.